// pages/user/user_browse/user_browse.js
const common = require('../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: null,
        nowPage: 0,
        isLoading: false,
        isAll: false,
        dataList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.type) {
            this.setData({
                type: options.type
            })
        }
        this.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    getList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        let that = this;
        let userId = wx.getStorageSync("userId");
        let dataList = this.data.dataList
        let data
        if (this.data.type == 'company') {
            data = {
                limit: 10,
                page: this.data.nowPage
            }
        } else {
            data = {
                belong_refer: userId,
                limit: 10,
                page: this.data.nowPage
            }
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Userbrowse/user_browse', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading();
            if (res.code == 204) {
                that.setData({
                    dataList: dataList,
                    isAll: true
                })
            } else {
                dataList = dataList.concat(res)
                that.setData({
                    dataList: dataList,
                    nowPage: that.data.nowPage + 1
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getList()
    },

    phoneCall: function(e) {
        if (e.currentTarget.dataset.phone) {
            wx.makePhoneCall({
                phoneNumber: e.currentTarget.dataset.phone
            })
        }
    },

    goDetail: function(e) {
        wx.navigateTo({
            url: '/pages/user/user_browseDetail/user_browseDetail?userId=' + e.currentTarget.dataset.id
        })
    }
})